//用户操作数据模块
//数据操作模块
var sqlHelper = require('../conf/sqlHelper');


//获取用户分页数据
function getCustomerListPager(currentPage,pageSize,callback){
    //计算起始数据
    //起始计数从0开始
    var startIndex=(currentPage-1)*pageSize;

    var sqlstr='SELECT *,DATE_FORMAT(reg_datetime,"%Y-%m-%d %H:%i:%s") as reg_time FROM shop_customer LIMIT ?,?';
    var param=[startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results[0].reg_time);
            callback(results);
        }
    }); 
}

//获取用户总行数
function getCustomerCounts(callback){
    var sqlstr='select count(*) as count from shop_customer';

    sqlHelper.ExecuteReader(sqlstr,null,function(results){
        if(results){
            //console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

//根据搜索关键字获取用户分页数据
function getSearchCustomerListPager(searchkey,currentPage,pageSize,callback){
    //计算起始数据
    //起始计数从0开始
    var startIndex=(currentPage-1)*pageSize;

    var sqlstr='SELECT *,DATE_FORMAT(reg_datetime,"%Y-%m-%d %H:%i:%s") as reg_time FROM shop_customer where loginname like ? LIMIT ?,?';
    var param=['%'+searchkey+'%',startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results[0].reg_time);
            callback(results);
        }
    }); 
}

function getSearchCustomerCounts(searchkey,callback){
    var sqlstr='select count(*) as count from shop_customer where loginname like ?';
    var param=['%'+searchkey+'%'];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

exports.getCustomerListPager=getCustomerListPager;
exports.getCustomerCounts=getCustomerCounts;
exports.getSearchCustomerListPager=getSearchCustomerListPager;
exports.getSearchCustomerCounts=getSearchCustomerCounts;